package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UaAppraiser;
import com.csun.cmny.provider.model.dto.dept.EmpLifeDto;
import com.csun.cmny.provider.model.dto.dept.EmpQueryDto;
import com.csun.cmny.provider.model.dto.emp.UacEditEmpDto;
import com.csun.cmny.provider.model.dto.life.UacLifeDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.model.vo.EmpVo;
import com.csun.cmny.provider.model.vo.LifeEmpVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UaAppraiserService;
import com.csun.cmny.provider.service.UacEmpService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-12-13 17:15
 * @Description:
 */
@RestController
@RequestMapping(value = "/emp", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacEmpMainController extends BaseController {

    @Resource
    private UacEmpService uacEmpService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UaAppraiserService appraiserService;
    @Resource
    private UacUserService userService;

    /**
     * 查询部门员工信息列表 - 分页
     */
    @RequestMapping(value = "/queryEmpListWithPage", method = RequestMethod.POST)
    public Wrapper queryEmpListWithPage(@RequestBody EmpQueryDto empQueryDto) {

        empQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacEmpService.queryEmpListWithPage(empQueryDto);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 查询部门员工信息列表 - 分页(带角色)
     */
    @RequestMapping(value = "/queryUserListByEmpWithPage", method = RequestMethod.POST)
    public Wrapper queryUserListByEmpWithPage(@RequestBody EmpQueryDto empQueryDto) {

//        empQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacEmpService.queryUserListByEmpWithPage(empQueryDto);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * @date: 2020-1-18 09:32:10
     * @author: Lijun
     * 查询部门员工信息列表 - 分页(多角色)
     */
    @RequestMapping(value = "/selectUserListByEmpWithPage", method = RequestMethod.POST)
    public Wrapper selectUserListByEmpWithPage(@RequestBody EmpQueryDto empQueryDto) {

        PageInfo pageInfo = uacEmpService.selectUserListByEmpWithPage(empQueryDto);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 查询指定部门的员工信息
     *
     * @param deptId 部门Id
     * @return 员工信息
     */
    @RequestMapping(value = "/getEmpByDeptId/{deptId}", method = RequestMethod.POST)
    public Wrapper<List<EmpVo>> getEmpByDeptId(@PathVariable  Long deptId) {

        List<EmpVo> empVoList = uacEmpService.getEmpByDeptId(deptId);

        //return WrapMapper.ok(empVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), empVoList);
    }

    /**
     * 查询指定部门的员工信息(包含角色)
     *
     * @param deptId 部门Id
     * @return 员工信息
     */
    @RequestMapping(value = "/getEmpByDeptIdWithRole/{deptId}", method = RequestMethod.POST)
    public Wrapper<List<EmpVo>> getEmpByDeptIdWithRole(@PathVariable  Long deptId) {

        List<EmpVo> empVoList = uacEmpService.getEmpByDeptIdWithRole(deptId);

        //return WrapMapper.ok(empVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), empVoList);
    }

    /**
     * 添加员工
     *
     * @param uacEditEmpDto 员工信息
     * @return 影响条数
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper<Integer> addUacEmp(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.addUacEmp(uacEditEmpDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 添加员工(包含角色)
     *
     * @param uacEditEmpDto 员工信息
     * @return 影响条数
     */
    @RequestMapping(value = "/saveWithRole", method = RequestMethod.POST)
    public Wrapper<Integer> addUacEmpWithRole(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.addUacEmpWithRole(uacEditEmpDto, getLoginAuthDto());



        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /*
    * 添加员工(多个角色)*/
    @RequestMapping(value = "/saveWithRoles", method = RequestMethod.POST)
    public Wrapper<Integer> addUacEmpWithRoles(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.addUacEmpWithRoles(uacEditEmpDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 编辑员工
     *
     * @param uacEditEmpDto 员工信息
     * @return 影响条数
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper<Integer> editUacEmp(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.editUacEmp(uacEditEmpDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 编辑员工（带角色）
     *
     * @param uacEditEmpDto 员工信息
     * @return 影响条数
     */
    @RequestMapping(value = "/editWithRole", method = RequestMethod.POST)
    public Wrapper<Integer> editWithRole(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.editUacEmpWithRole(uacEditEmpDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 编辑员工(带多个角色)
     * @param uacEditEmpDto
     * @return
     */
    @RequestMapping(value = "/editWithRoles", method = RequestMethod.POST)
    public Wrapper<Integer> editWithRoles(@RequestBody UacEditEmpDto uacEditEmpDto) {

        int result = uacEmpService.editUacEmpWithRoles(uacEditEmpDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据userId查询员工信息
     *
     * @param userId 用户Id
     * @return 员工信息
     */
    @RequestMapping(value = "/getUacEmpByUserId/{userId}", method = RequestMethod.POST)
    public Wrapper<EmpVo> getUacEmpByUserId(@PathVariable Long userId) {

        EmpVo empVo = uacEmpService.getUacEmpByUserId(userId);

        //return WrapMapper.ok(empVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), empVo);
    }

    /**
     * 根据userId查询员工信息(带角色)
     *
     * @param userId 用户Id
     * @return 员工信息
     */
    @RequestMapping(value = "/getUacEmpWithRoleByUserId/{userId}", method = RequestMethod.POST)
    public Wrapper<EmpVo> getUacEmpWithRoleByUserId(@PathVariable Long userId) {

        EmpVo empVo = uacEmpService.getUacEmpWithRoleByUserId(userId);

        //return WrapMapper.ok(empVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), empVo);
    }

    /**
     * 根据用户Id删除员工信息
     *
     * @param userId 用户Id
     * @return 影响条数
     */
    @RequestMapping(value = "/deleteUacEmpByUserId/{userId}", method = RequestMethod.POST)
    public Wrapper<Integer> deleteUacEmpByUserId(@PathVariable Long userId) {

        int result = uacEmpService.deleteUacEmpByUserId(userId);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据用户Id删除员工信息（带角色）
     *
     * @param userId 用户Id
     * @return 影响条数
     */
    @RequestMapping(value = "/deleteUacEmpWithRoleByUserId/{userId}", method = RequestMethod.POST)
    public Wrapper<Integer> deleteUacEmpWithRoleByUserId(@PathVariable Long userId) {

        int result = uacEmpService.deleteUacEmpWithRoleByUserId(userId);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }


    /**
     * lefe查询部门员工信息列表
     */
    @RequestMapping(value = "/queryUserListByEmpWithLife", method = RequestMethod.POST)
    public Wrapper queryUserListByEmpWithLife(@RequestBody EmpLifeDto empLifeDto) {

        empLifeDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacEmpService.queryUserListByEmpWithLife(empLifeDto);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 添加Life工作人员
     * @return 影响条数
     */
    @RequestMapping(value = "/addLifeUser", method = RequestMethod.POST)
    public Wrapper<Integer> addLifeUser(@RequestBody UacLifeDto uacLifeDto) {

        int result = uacEmpService.addLifeUser(uacLifeDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 删除Life员工
     */
    @RequestMapping(value = "/deleteLifeUser/{userId}", method = RequestMethod.POST)
    public Wrapper<Integer> deleteLifeUser(@PathVariable Long userId) {
        if(getLoginAuthDto().getUserId().equals(userId)){
            throw new UacBizException(ErrorCodeEnum.UAC10017006);
        }
        int result = uacEmpService.deleteLifeByUserId(userId);
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询Life员工详情
     */
    @RequestMapping(value = "/selectLifeUser/{userId}", method = RequestMethod.POST)
    public Wrapper<LifeEmpVo> selectLifeUser(@PathVariable Long userId) {
        LifeEmpVo lifeEmpVo = uacEmpService.selectLifeUser(userId);
        //return WrapMapper.ok(lifeEmpVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), lifeEmpVo);
    }

    /**
     * 编辑员工（带角色）
     *
     * @param uacLifeDto 员工信息
     * @return 影响条数
     */
    @RequestMapping(value = "/updateLifeUser", method = RequestMethod.POST)
    public Wrapper<Integer> updateLifeUser(@RequestBody UacLifeDto uacLifeDto) {

        int result = uacEmpService.updateLifeUser(uacLifeDto, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }
}
